<?php

set_time_limit(0);
function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}
require_once('rb.php');
R::setup('mysql:host=ftp.giemiebier.nl:3306;dbname=personify', 'school', 'admin123');
$users = R::getAll('select * from users');
$tmp_users = $users;
//$users = R::getAll('select * from users where emailaddress LIKE :email', array(':email' => "%erik@erik-vd-veen.nl%"));

$total_chats = 0;
$total_msg = 0;
foreach ($users as $user) {
    $user_id = $user["id"];
    echo $user["emailaddress"] . " " . $user["id"];
    $url_create_chat = 'http://erik.erik-vd-veen.nl/hacog/create_chat';
    $url_create_msg = 'http://erik.erik-vd-veen.nl/hacog/send_message';
    $amount_chats = mt_rand(0, 5);
    for ($i = 0; $i < $amount_chats; $i++) {
        shuffle($tmp_users);
        $user2 = array_rand($tmp_users);
        $email2 = $tmp_users[$user2]["emailaddress"];
        $data = array('id' => $user_id, 'email' => $email2);

        $options = array(
            'http' => array(
                'header' => "Content-type: application/x-www-form-urlencoded\r\n",
                'method' => 'POST',
                'content' => http_build_query($data),
            ),
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url_create_chat, false, $context);
        $return_arr = json_decode($result);
        //var_dump($return_arr);
        $chat_id = $return_arr->id;
        //var_dump($chat_id);
        $amount_msg = mt_rand(1, 20);
        for ($i = 0; $i < $amount_msg; $i++) {
            $message = generateRandomString();
            $data = array('sender_id' => $user_id, 'message' => $message, 'chat_id' => $chat_id);
            $options = array(
                'http' => array(
                    'header' => "Content-type: application/x-www-form-urlencoded\r\n",
                    'method' => 'POST',
                    'content' => http_build_query($data),
                ),
            );
            $context = stream_context_create($options);
            $result = file_get_contents($url_create_msg, false, $context);
            $total_msg++;
        }
        echo " created chat with: " . $email2;
        $total_chats++;
    }
    echo "<br>";
}
echo "<br><br>created chats: " . $total_chats;
echo "<br>created msg: " . $total_msg;

